package com.dgshanger.sanhxiaofeisc.ui.person.purse

import android.annotation.SuppressLint
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AnimationUtils
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.entity.HistoryTransferEntity
import com.dgshanger.sanhxiaofeisc.entity.TransferAccountsEntity
import com.dgshanger.sanhxiaofeisc.entity.UserEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.TransferAccountsContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.TransferAccountsPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.person.setting.EditPayPasswordActivity
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import com.xxl.sodmall.dialog.WalletPayDialogWrapper
import kotlinx.android.synthetic.main.activity_pay_model_setting.*
import kotlinx.android.synthetic.main.activity_transfer_accounts.*
import kotlinx.android.synthetic.main.item_history_aid_list.view.*


/**
 * TransferAccountsActivity
 * 沉迷学习不能自拔
 * Describe：转账页面
 * Created by 雷小星🍀 on 2018/4/18 11:40.
 */
class TransferAccountsActivity : BaseMvpActivity<TransferAccountsContract.Model, TransferAccountsContract.View, TransferAccountsPresenter>(),
        TransferAccountsContract.View {

    override fun setUserData(userData: UserEntity?) {
        ImageLoader.loadAvatar(mContext, iv_avatar, userData?.avatar)
        tv_nick_name.text = userData?.nick_name
//        et_payee_user_mobile.setText(userData?.mobile)
    }

    private var mPayPwd : String = ""

    override fun getRemark(): String = et_remark.text.toString().trim()

    override fun getPayeeUserMobile(): String = et_payee_user_mobile.text.toString().trim()

    override fun getWalletMoney(): String = et_user_wallet_money.text.toString().trim()

    override fun getIntegralNumber(): String = et_user_integral_total.text.toString().trim()

    override fun getPayPwd(): String = mPayPwd

    @SuppressLint("SetTextI18n")
    override fun setData(entity: TransferAccountsEntity?) {
        with(entity!!) {
            et_user_wallet_money.hint = getString(R.string.cash_balance) + user_wallet_money
            et_user_integral_total.hint = getString(R.string.Bonus_points_balance) + user_integral_total
        }
        view_content.removeAllViews()
        val user_transfer_record = entity.user_transfer_record
        if (user_transfer_record == null || user_transfer_record.size == 0) {
            val emptyView = createHistoryView(null)
            view_content.addView(emptyView)
            emptyView.animation = AnimationUtils.makeInChildBottomAnimation(mContext)
        }
        for (mEntity in user_transfer_record) {
            val historyView = createHistoryView(mEntity)
            view_content.addView(historyView)
            historyView.animation = AnimationUtils.makeInChildBottomAnimation(mContext)
        }
    }

    fun createHistoryView(entity: HistoryTransferEntity?): View {
        if (entity == null) {
            //空布局
            return LayoutInflater.from(mContext).inflate(R.layout.item_view_empty, null)
        }
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_history_aid_list, null)
        with(entity) {
            view.tv_payee_user_show_id.text = "ID:" + payee_user_show_id
            view.tv_payee_user_nickname.text = payee_user_name
            ImageLoader.loadAvatar(view.context, view.iv_avatar, payee_user_avatar)
        }

        view.setOnClickListener {
            setUserData(UserEntity().apply {
                avatar = entity.payee_user_avatar
                nick_name = entity.payee_user_name
                et_payee_user_mobile.setText(entity.payee_user_mobile)
            })
        }

        return view
    }

    override fun createPresenter(): TransferAccountsPresenter = TransferAccountsPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_transfer_accounts

    private lateinit var historyTransferListFragment: HistoryTransferListFragment

    override fun initData() {
        getPresenter().loadData()
    }

    override fun initEvent() {
        btn_sure.setOnClickListener {
            //转账
           getPresenter().checkPayPwd()
        }
        et_payee_user_mobile.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                et_payee_user_mobile.setSelection(et_payee_user_mobile.length())//设置光标到最后一位
                if (et_payee_user_mobile.length() == 11) {
                    //手机号输入完整时进行查询
                    getPresenter().loadUser(s.toString())
                } else {
                    iv_avatar.setImageResource(R.color.colorDivler)
                    tv_nick_name.text = "_ _"
                }
            }

        })

    }

    override fun onSettingPayPwd() {
        val intent = Intent(mContext, EditPayPasswordActivity::class.java)
        startActivity(intent)
    }

    override fun showPayPwdDialog() {
        val walletPayDialogWapper = WalletPayDialogWrapper(mContext,walletMoney)
        if (!integralNumber.isEmpty()){
            walletPayDialogWapper.setIntegralText(integralNumber)
        }
        walletPayDialogWapper.setOnPasswordFinishListener(object : WalletPayDialogWrapper.OnPasswordFinishListener{
            override fun onPasswordFinishListener(password: String) {
                mPayPwd=password
                getPresenter().transfer()
                walletPayDialogWapper.dismissDialog()
            }
        })
        walletPayDialogWapper.showDialog()
    }
}